package com.sale.succ.system.txt;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import com.sale.succ.objects.ClientObj;
import com.sale.succ.objects.ClientObjCollection;
import com.sale.succ.objects.GroupObjCollection;
import com.sale.succ.objects.LastSalesObj;
import com.sale.succ.objects.LastSalesObjCollection;
import com.sale.succ.objects.MappingProductsToTradeMarkCollection;
import com.sale.succ.objects.OrderObj;
import com.sale.succ.objects.OrderObjCollection;
import com.sale.succ.objects.PrivateObjCollection;
import com.sale.succ.objects.ProductObj;
import com.sale.succ.objects.ProductObjCollection;
import com.sale.succ.objects.TradeMarkObjCollection;
import com.sale.succ.utils.SystemConstants;

public class ObjectsProvider {
	private ClientObjCollection clientObjCollection;
	private ProductObjCollection productObjCollection;
	private TradeMarkObjCollection tradeMarkObjCollection;
	private MappingProductsToTradeMarkCollection mappingProductsToTradeMarkCollection;
	private PrivateObjCollection privateObjCollection;
	private LastSalesObjCollection lastSalesObjCollection;
	private GroupObjCollection groupObjCollection;
	private OrderObjCollection orderObjCollection;

	private static ObjectsProvider _instance;
	
	public static ObjectsProvider getInstance(){
		if(_instance == null){
			_instance = new ObjectsProvider();
		}
		return _instance;
	}
	
	public void clearCollections(){
		productObjCollection = null;
		tradeMarkObjCollection = null;
		mappingProductsToTradeMarkCollection = null;
		privateObjCollection = null;
		groupObjCollection = null;
	}
	
	public ClientObjCollection getClientCollection(String groupId){
		//if(clientObjCollection == null){
			clientObjCollection = new ClientObjCollection();
			ArrayList list = TXTProcessing.getCollectionFromTXT(groupId + SystemConstants.CLIENTS_FILE);
			if(list != null && list.size() != 0){
				for(int i=0; i<list.size(); i++){
					clientObjCollection.put(
							(String)((ArrayList)list.get(i)).get(1),
							new ClientObj(
									(String)((ArrayList)list.get(i)).get(0),
									(String)((ArrayList)list.get(i)).get(1),
									(String)((ArrayList)list.get(i)).get(2),
									(String)((ArrayList)list.get(i)).get(3)
									)
					);
				}
			}
		//}
		return clientObjCollection;
	}
	
	public ProductObjCollection getProductCollection(){
		if(productObjCollection == null){
			productObjCollection = new ProductObjCollection();
			ArrayList list = TXTProcessing.getCollectionFromTXT(SystemConstants.PRODUCTS_FILE);
			if(list != null && list.size() != 0){
				for(int i=0; i<list.size(); i++){
					productObjCollection.put(
							(String)((ArrayList)list.get(i)).get(0),
							new ProductObj(
									(String)((ArrayList)list.get(i)).get(0),
									(String)((ArrayList)list.get(i)).get(1),
									(String)((ArrayList)list.get(i)).get(2)
								)
					);
				}
			}
			// set globalRest from file
			File file = new File(SystemConstants.TRADE_ZIP_DIR + File.separator + SystemConstants.RESTS_FILE);
			if(file.exists()){
				ArrayList rests = TXTProcessing.getCollectionFromTXT(SystemConstants.TRADE_ZIP_DIR + File.separator + SystemConstants.RESTS_FILE, true);
				if(rests.size() != 0){
					for(int i=0; i<rests.size(); i++){
						String prdId = (String)((ArrayList)rests.get(i)).get(0);
						String prdRest = (String)((ArrayList)rests.get(i)).get(1);
						if(productObjCollection.containsKey(prdId)){
							productObjCollection.get(prdId).setGlobalRest(Integer.parseInt(prdRest));
						}
					}
				}
			}
		}
		return productObjCollection;
	}
	
	public TradeMarkObjCollection getTradeMarkCollection(){
		if(tradeMarkObjCollection == null){
			tradeMarkObjCollection = new TradeMarkObjCollection();
			ArrayList list = TXTProcessing.getCollectionFromTXT(SystemConstants.MARKS_FILE);
			if(list != null && list.size() != 0){
				for(int i=0; i<list.size(); i++){
					tradeMarkObjCollection.put((String)((ArrayList)list.get(i)).get(0), (String)((ArrayList)list.get(i)).get(1));
				}
			}
		}
		return tradeMarkObjCollection;
	}
	
	public MappingProductsToTradeMarkCollection getMappingProductsToTradeMarkCollection(){
		if(mappingProductsToTradeMarkCollection == null){
			mappingProductsToTradeMarkCollection = new MappingProductsToTradeMarkCollection();
			ArrayList list = TXTProcessing.getCollectionFromTXT(SystemConstants.TPR_FILE);
			String key, value;
			if(list != null && list.size() != 0){
				for(int i=0; i<list.size(); i++){
					key = (String)((ArrayList)list.get(i)).get(0);
					value = (String)((ArrayList)list.get(i)).get(1);
					if(mappingProductsToTradeMarkCollection.containsKey(key)){
						mappingProductsToTradeMarkCollection.get(key).add(value);
					}
					else {
						ArrayList temp = new ArrayList();
						temp.add(value);
						mappingProductsToTradeMarkCollection.put(key, temp);
					}
				}
			}
		}
		return mappingProductsToTradeMarkCollection;
	}
	
	public PrivateObjCollection getPrivateCollection(){
		if(privateObjCollection == null){
			privateObjCollection = new PrivateObjCollection();
			ArrayList list = TXTProcessing.getCollectionFromTXT(SystemConstants.PRIVATE_FILE);
			if(list != null && list.size() != 0){
				for(int i=0; i<list.size(); i++){
					privateObjCollection.put((String)((ArrayList)list.get(i)).get(0), (String)((ArrayList)list.get(i)).get(1));
				}
			}
		}
		return privateObjCollection;
	}
	
	public void recreateLastSaleCollection(String clientId){
		lastSalesObjCollection = null;
		getLastSalesCollection(clientId);
	}
	
	public LastSalesObjCollection getLastSalesCollection(String clientId) {
		if (lastSalesObjCollection == null) {
			lastSalesObjCollection = new LastSalesObjCollection();
			ArrayList list = TXTProcessing.getCollectionFromTXT(clientId
					+ SystemConstants.LAST_SALE_FILE);
			String _clientId, productId, sales1, sales2, price;
			if (list != null && list.size() != 0) {
				for (int i = 0; i < list.size(); i++) {
					_clientId = (String) ((ArrayList) list.get(i)).get(0);
					productId = (String) ((ArrayList) list.get(i)).get(1);
					sales1 = (String) ((ArrayList) list.get(i)).get(2);
					sales2 = (String) ((ArrayList) list.get(i)).get(3);
					price = (String) ((ArrayList) list.get(i)).get(4);
					lastSalesObjCollection.put(productId, new LastSalesObj(
							_clientId, productId, sales1, sales2, price));
				}
			}
		} else {
			Set set = lastSalesObjCollection.keySet();
			Iterator it = set.iterator();
			while (it.hasNext()) {
				LastSalesObj lastObj = lastSalesObjCollection.get((String) it
						.next());
				lastObj.setOrder("0");
			}
		}
		return lastSalesObjCollection;
	}	
	
	public GroupObjCollection getGroupCollection(){
		if(groupObjCollection == null){
			groupObjCollection = new GroupObjCollection();
			ArrayList list = TXTProcessing.getCollectionFromTXT(SystemConstants.GROUPS_FILE);
			if(list != null && list.size() != 0){
				for(int i=0; i<list.size(); i++){
					groupObjCollection.put((String)((ArrayList)list.get(i)).get(0), (String)((ArrayList)list.get(i)).get(1));
				}
			}
		}
		return groupObjCollection;
	}
	
	public OrderObjCollection getOrderObjCollection(String clientId) {
		orderObjCollection = new OrderObjCollection();
		ArrayList orders = TXTProcessing.getCollectionFromTXT(
				SystemConstants.ORDERS_DIR + File.separator + clientId + SystemConstants.ORDER_FILE,
				true
				);
		if (orders != null && orders.size() != 0) {
			for (int i = 0; i < orders.size(); i++) {
				ArrayList order = (ArrayList) orders.get(i);
				OrderObj obj = new OrderObj();
				obj.setClientId((String) order.get(0));
				obj.setTradeId((String) order.get(1));
				obj.setProductId((String) order.get(2));
				obj.setPrice((String) order.get(3));
				obj.setOrder((String) order.get(4));
				obj.setFwId((String) order.get(5));
				obj.setDateGive((String) order.get(6));
				obj.setCertificates((String) order.get(7));
				obj.setPayDate((String) order.get(8));
				obj.setMoneyCondition((String) order.get(9));
				obj.setTablet((String) order.get(10));
				obj.setExchange((String) order.get(11));
				obj.setNotes((String) order.get(12));
				obj.setAsFact((String) order.get(13));
				orderObjCollection.add(obj);
			}
		}
		return orderObjCollection;
	}
	
	public boolean saveOrderObjectCollection(String clientId, ArrayList orders, boolean toEnd) {
		return TXTProcessing.getTXTFromCollection(
				orders,
				SystemConstants.ORDERS_DIR + File.separator + clientId + SystemConstants.ORDER_FILE,
				toEnd);
	}

}
